package barbershop.com.service.impl;

import barbershop.com.entity.ServiceType;
import barbershop.com.dao.ServiceTypeDao;
import barbershop.com.service.ServiceTypeService;
import barbershop.com.service.ServicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ServiceTypeServiceImpl implements ServiceTypeService {
    @Autowired
    private ServiceTypeDao serviceTypeDao;

    @Override
    public ServiceType insert(ServiceType record) {
        ServiceType serviceType = new ServiceType();
        try {
            serviceType = this.serviceTypeDao.save(record);
            System.out.println("插入成功！");
            return serviceType;
        } catch (Exception e){
            System.out.println("插入失败！");
            e.printStackTrace();

        }
        return null;
    }

    @Override
    public Boolean delete(ServiceType record) {
        try {
            this.serviceTypeDao.delete(record);
            System.out.println("删除成功！");
            return true;
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("删除失败");
            return false;
        }
    }

    @Override
    public ServiceType update(ServiceType record) {
        ServiceType serviceType = new ServiceType();
        try {
            serviceType = this.serviceTypeDao.save(record);
            System.out.println("修改成功！");
            return serviceType;
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("修改失败！");
        }
        return null;
    }

    @Override
    public ServiceType find(ServiceType record) {
        Optional<ServiceType> serviceType;
        try {
            serviceType = this.serviceTypeDao.findById(record.getServiceTypeId());
            if (serviceType.isPresent())
                System.out.println("查找成功！");
                return serviceType.get();
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("查找失败");
        }
        return null;
    }

    @Override
    public List<ServiceType> findAll() {
        try {
            List<ServiceType> serviceTypeList
                    = this.serviceTypeDao.findAll();
            System.out.println("所有服务类型获取成功！");
            return serviceTypeList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("所有服务类型获取失败！");
        }
        return null;
    }
}
